<?php
/**
 *	description:ZMAXWECHAT 推送管理控制器文件
 *  author：min.zhang
 *  Email:zhang19min88@163.com
 *	Url:http://www.zmax99.com
 *  copyright:南宁市程序人软件科技有限责任公司保留所有权利
 *  date:2016-04-28
 *  @license GNU General Public License version 3, or later
 *  check date:2016-05-26
 *  checker :min.zhang
 */
 
defined('_JEXEC') or die('You can not access this file!');

jimport('joomla.application.component.controllerform');
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/token.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/media.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/news.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/material.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/db.php");
class zmaxwechatControllerSended extends JControllerForm
 {	
	public function send()
	{
		$app = JFactory::getApplication();
		$ids = $app->input->get("cid" ,'','ARRAY');
		$id = $ids[0];
		
		//通过ID 获得media_id
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select("media_id")->from("#__zmaxwechat_news")->where("id=".$id);
		$db->setQuery($query);
		$item = $db->loadObject();
		
		//检查一下是否该消息已经被提交
		$query = $db->getQuery(true);
		$query->select("date")->from("#__zmaxwechat_sended")->where("media_id='".$item->media_id."'");
		$db->setQuery($query);
		$result = $db->loadObject();
		if($result) //如果已经提交了
		{
			$msg="该素材已经于".$result->date."完成了群发功能!";
			$this->setRedirect("index.php?option=com_zmaxwechat&view=medias" ,$msg,"warning");
			return true;
		}
		
		//执行群发功能
		$token = new zmaxwechatToken();
		$token = $token->getToken();
		$media = new zmaxwechatMedia($token->token);
		
		//准备群发过滤器
		$filter = new stdclass();
		$filter->is_to_all=true;
		$filter->tag_id="";
		
		$news = new stdclass();
		$news->media_id = $item->media_id;
		
		$result = $media->sendNews($filter ,$news );
		
		if($result->errcode!=0) //发送失败
		{
			$msg="提交群发任务失败:".$json_encode($result);
			$this->setRedirect("index.php?option=com_zmaxwechat&view=medias" ,$msg,"error");
			return false;
		}
		
		//群发成功，需要将这些内容存储起来
		$sended = new stdclass();
		$sended->submit_state=1;
		$sended->media_id =$item->media_id;
		$sended->date = JDate::getInstance()->toSql();
		$sended->result = json_encode($result);
		$sended->msg_id = $result->msg_id;
		$sended->msg_data_id = $result->msg_data_id;
		
		$db->insertObject("#__zmaxwechat_sended" ,$sended);
		$msg="提交群发任务成功！";
		$this->setRedirect("index.php?option=com_zmaxwechat&view=sendeds" ,$msg);
		return true;
		
		
		
	}
	public function save($key = NULL, $urlVar = NULL)
	{
		$app = JFactory::getApplication();
		$data = $app->input->get('jform','','ARRAY');
		
		$token = new zmaxwechatToken();
		$token = $token->getToken();
		
		$media = new zmaxwechatMedia($token->token);
		

		$news = array();
		for($i=0 ;$i<8;$i++)
		{
			$name="new".$i;
			if(isset($data[$name]) && is_array($data[$name]))
			{
				if($data[$name]["title"] && $data[$name]["content"])
				{
					$news[]=$data[$name];
				}
				
			}
		}
		
		$content = new stdclass();
		$content->news_item=$news;
		$content->create_time = JDate::getInstance()->toUnix();
		$content->update_time = JDate::getInstance()->toUnix();
		
		$data["content"] = json_encode($content);

		
		
		
		$id = 1;
		$article =JTable::getInstance("content");
		$article->load($id);
		
		
	
		
		$filePath = "/images/headers/windows.jpg";
		
		$material = new zmaxwechatMaterial($token->token);
		$info = $material->getMediaInfo($filePath);
		
		$new = new stdclass();
		$new->title=$article->title;
		$new->thumb_media_id = json_decode($info)->media_id;
		$new->author="ZMAX WECHAT";
		$new->digest=mb_strimwidth($article->introtext,0,40);
		$new->show_cover_pic=1;
		$new->content=$article->introtext.$article->fulltext;
		$new->content_source_url="http://www.joomlachina.cn";
		
		$news[]=$new;

		$result = $media->addNews($news);
		
		
		$filter = new stdclass();
		$filter->is_to_all=true;
		$filter->tag_id="";
		
		$news = new stdclass();
		$news->media_id = $result->media_id;
		
		$result = $media->sendNews($filter ,$news );
		
		echo "<pre>";
			print_r($result);
		echo "</pre>";
		
		
		
		/*
		$filePath = JPath::clean($filePath);
		$result = $media->addImageToServer($filePath);
		$result = json_decode($result);
		echo "<pre>";
			print_r($result);
		echo "</pre>";
		*/
		
		
	}
 }	
	

?>